<?php

namespace App\Repository\Repositories;

use App\Models\WechatUser;
use App\Helpers\Code;
use App\Helpers\Token;
use App\Http\Controllers\Controller;
use App\Http\Service\Redisd;
use App\Models\User;
use App\Repository\RepositoryInterface\UserLoginInterface;
use Illuminate\Database\QueryException;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Redis;
use Illuminate\Support\Facades\Validator;

class UserLoginRepositories  implements UserLoginInterface
{

    public function perDetails($uid)
    {
        return WechatUser::with(['systemStoreStaff', 'systemStoreStaff.systemStore'])->where('uid', $uid)->first();
    }
    /**
     * @param $phone @用户登录注册获取验证码的手机号
     *
     */
    public function sendCode($phone)
    {
        $validator = Validator::make(['phone'=>$phone], [
            'phone' => 'regex:/^1[3579]\d{9}$/|required',
        ]);

        if($validator->fails()){
            return showMsg(500,'手机号格式不正确或者为空',[]);
        }

        $result=Redis::exists($phone.'last_time');
        if($result !=0 ){
            $lastTime=Redisd::getInstance()->get($phone.'last_time');
            if(time()-$lastTime < 180){
                return  showMsg(500,'3分钟之内请勿重复发送',[]);
            }
        }
        $code=rand(1000,9999);
        $res=(new Code())->getCode($phone,$code,'yishengcool',md5('802313ys'));
        if($res != "短信发送成功"){
            return showMsg(500,$res,[]);
        }
        Redisd::getInstance()->set($phone.'code',$code,300);
        Redisd::getInstance()->set($phone.'last_time',time());
        return  showMsg(200,'发送成功',[]);
    }

    /**
     * @param $data
     * 用户注册方法
     */
    public function userRegister ($data)
    {
        $result=Redis::exists($data['phone'].'code');
        if($result != 0){
            $code=Redisd::getInstance()->get($data['phone'].'code');
            if($code != $data['code']){
                return  showMsg(500,'验证码有误',[]);
            }
            //验证码正确 判断手机号是否存在
            $user_data=User::where('phone',$data['phone'])->first();
            if(!empty($user_data)){
                return showMsg(500,'手机号已经注册过账号',[]);
            }
            $data['password']=bcrypt($data['password']);
            unset($data['code']);
            try {
                $res=User::create($data);
                return showMsg(200,'注册成功',[]);
            }catch (QueryException $queryException){
                return  showMsg(500,$queryException->getMessage(),[]);
            }

        }
        return showMsg(500,'验证码失效,请重新获取',[]);
    }

    /**
     * @param $data
     * 校验账号密码
     */
    public function verify($data)
    {
        $validator=Validator::make($data,[
            'account'=>'required',
            'password'=>'required'
        ]);
        if($validator->fails()){
            return  showMsg();
        }
        $bool=Auth::attempt($data);
        if(!$bool){
           return showMsg(500,'账号或密码有误',[]);
        }
        $user_data=User::where('account',$data['account'])->first();
        $jwtToken=Token::createToken($user_data->uid);
        return  showMsg(200,'登录成功',$jwtToken);
    }

    public function upUserInfo($data)
    {
        $res=Token::verifyToken($data['jwtToken']);
        try {
            unset($data['jwtToken']);
            User::where('uid',$res)->update($data);
            return  showMsg(200,'用户信息修改成功',[]);
        }catch (QueryException $queryException){
            return showMsg($queryException->getMessage());
        }
    }
}
